package com.hitqz.robot.biz.flow.component.oilSample;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hitqz.robot.api.business.enums.BrandEnum;
import com.hitqz.robot.api.business.enums.EquipmentEnum;
import com.hitqz.robot.api.common.entity.RobotEquipment;
import com.hitqz.robot.biz.service.RobotEquipmentService;
import com.hitqz.robot.camera.EquipmentFactory;
import com.hitqz.robot.camera.IEquipmentService;
import com.hitqz.robot.camera.configure.EquipmentCmdEnum;
import com.hitqz.robot.camera.dto.DoorCtrlDto;
import com.hitqz.robot.camera.dto.EquipmentParamsDto;
import com.hitqz.robot.camera.dto.LeftRightReachDto;
import com.hitqz.robot.camera.dto.QueryParamsDto;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Optional;

/**
 * @author xupkun
 * @date 2024/10/4
 */
@LiteflowComponent(name = "[取油样机器人]取油柜-模组对接", value = "MathModuleComponent")
@RequiredArgsConstructor
@Slf4j
public class MathModuleComponent extends NodeComponent {
    private final RobotEquipmentService robotEquipmentService;

    private final EquipmentFactory equipmentFactory;


    @Override
    public void process() throws Exception {
        List<RobotEquipment> oils = robotEquipmentService.list(Wrappers.<RobotEquipment>lambdaQuery()
                .eq(RobotEquipment::getType, EquipmentEnum.PLC.getType())
                .eq(RobotEquipment::getBrand, BrandEnum.OIL_PLC.getEnStr())
                .eq(RobotEquipment::getIsRecordParam, 1));

        List<RobotEquipment> carPlc = robotEquipmentService.list(Wrappers.<RobotEquipment>lambdaQuery()
                .eq(RobotEquipment::getType, EquipmentEnum.PLC.getType())
                .eq(RobotEquipment::getBrand, BrandEnum.CAR_PLC.getEnStr())
                .eq(RobotEquipment::getIsRecordParam, 1));
        RobotEquipment robotEquipment = carPlc.get(0);
        QueryParamsDto carDto = new QueryParamsDto();
        carDto.setPassword(robotEquipment.getPassword());
        carDto.setUsername(robotEquipment.getUsername());
        carDto.setPort(robotEquipment.getPort());
        carDto.setChannel(robotEquipment.getChannel());
        carDto.setIp(robotEquipment.getIp());
        for (RobotEquipment oil : CollUtil.emptyIfNull(oils)) {
            // 柜子开始对
            IEquipmentService service =  equipmentFactory.getServiceByBrand(oil.getBrand());
            QueryParamsDto queryParamsDto = new QueryParamsDto();
            queryParamsDto.setPassword(oil.getPassword());
            queryParamsDto.setUsername(oil.getUsername());
            queryParamsDto.setPort(oil.getPort());
            queryParamsDto.setChannel(oil.getChannel());
            queryParamsDto.setIp(oil.getIp());
            DoorCtrlDto doorCtrlDto = new DoorCtrlDto();
            doorCtrlDto.setDoor(1);
            service.executeCmd(queryParamsDto , false , EquipmentCmdEnum.START_MATCH, doorCtrlDto);
            // 查询对接信息
            // 转发信号
            while (true) {
                List<EquipmentParamsDto> equipmentParamsDtos =  service.getEquipmentParams(queryParamsDto);
                Optional<EquipmentParamsDto> dto = equipmentParamsDtos.stream().filter(t-> t.getKey().equals("match")).findFirst();
                if (!dto.isPresent()) {
                    continue;
                }
                if (dto.get().getValue().equals("1")) {
                    break;
                }
                if (dto.get().getValue().equals("2")) {
                    log.info("对接失败");
                    break;
                }
                List<EquipmentParamsDto> carParamsDto =  equipmentFactory.getServiceByBrand(robotEquipment.getBrand()).getEquipmentParams(carDto);
                // 转发信号
                Optional<EquipmentParamsDto> left = carParamsDto.stream().filter(t-> t.getKey().equals("left")).findFirst();
                Optional<EquipmentParamsDto> right = carParamsDto.stream().filter(t-> t.getKey().equals("right")).findFirst();
                if (right.isPresent() && left.isPresent()) {
                    log.info("Left {} and right {}", left.get().getValue(), right.get().getValue());
                    LeftRightReachDto leftRightReachDto = new LeftRightReachDto();
                    leftRightReachDto.setLeft(Integer.parseInt(left.get().getValue()));
                    leftRightReachDto.setRight(Integer.parseInt(right.get().getValue()));
                    service.executeCmd(queryParamsDto, false, EquipmentCmdEnum.LEFT_RIGHT, leftRightReachDto);
                }
            }




        }



    }
}
